//
//  Data.swift
//  newsApp
//
//  Created by Doo Psyist on 2020/5/12.
//  Copyright © 2020 Doo Psyist. All rights reserved.
//

import SwiftUI

let newsList: [NewsListItem] = load("newsList.json")
//var ws = WebService()
//let resumeList: [ResumeListItem] = loadStr(ws.fetchResumeList())

func load<T: Decodable>(_ filename: String) -> T {
    let data: Data
    
    guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
        else {
            fatalError("Couldn't find \(filename) in main bundle.")
    }
    
    do {
        data = try Data(contentsOf: file)
    } catch {
        fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
    }
    
    do {
        let decoder = JSONDecoder()
        return try decoder.decode(T.self, from: data)
    } catch {
        fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
    }
}

func loadStr<T: Decodable>(_ str: String) -> T {
    
    let data: Data = str.data(using: .utf8)!
    
    do {
        let decoder = JSONDecoder()
        return try decoder.decode(T.self, from: data)
    } catch {
        fatalError("Couldn't parse")
    }
}

